﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;

public partial class In_Maintenance : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {

    }
    protected void submitButton_Click(object sender, EventArgs e) {
        string status = "Activated";
        string email = String.Empty;
        string user_id = String.Empty;
        string subject = "i2iPortal Subscriber Confirmation";
        string strMsg = "Thank you for your interest in i2iPortal. You will be the first to know when we are ready for launch.";

        //to retrieve only Filename from the complete path
        int extractPos1 = emailTextBox.Text.LastIndexOf(",") - 1;
        string newEmail = emailTextBox.Text.Substring(1, extractPos1);
        if (newEmail.IndexOf(",") >= 0 || newEmail.IndexOf("\\,") >= 0) {
            //We found a / or \
            Response.End();
        }
        try {
            CreateConn objcon = new CreateConn();
            string selectQuery = "SELECT email FROM Subscribers WHERE email = '" + newEmail + "'";
            DataTable dt = objcon.SQLConnDataTable(selectQuery);
            email = dt.Rows[0]["email"].ToString();
           
        } catch (Exception err) {
            Console.Write("Err msg:" + err.Message);
            submitStatusLabel.Text = "please try again...";
        }
        if (email == newEmail) {
            try {
                CreateConn objcon = new CreateConn();
                string selectQuery = "SELECT user_id FROM Users WHERE email = '" + newEmail + "'";
                DataTable dt = objcon.SQLConnDataTable(selectQuery);
                user_id = dt.Rows[0]["user_id"].ToString();
                string updateQuery = String.Empty;
                if (user_id != String.Empty) {
                    updateQuery = "UPDATE Subscribers" +
                        " SET dateAltered = '" + DateTime.Now + "',status = '" + status + "',user_id = '" + user_id + "'" +
                        " WHERE  (email = '" + newEmail + "')";
                } else {
                    updateQuery = "UPDATE Subscribers" +
                        " SET dateAltered = '" + DateTime.Now + "',status = '" + status + "'" +
                        " WHERE  (email = '" + newEmail + "')";
                }
                Email objmail = new Email();
                objcon.SQLCmd(updateQuery);
                if (!objmail.SendEmail(newEmail, subject, strMsg)) {
                    submitStatusLabel.Text = "please try again...";
                }
            } catch (Exception err) {
                Console.Write("Err msg:" + err.Message);
            }
        } else {
            try {
                CreateConn objcon = new CreateConn();
                string insertQuery = String.Empty;
                if (user_id != String.Empty) {
                    insertQuery = "INSERT INTO Subscribers (dateRecorded, email, status, user_id)"
                           + " values ('" + DateTime.Now + "','" + newEmail + "','" + status + "', '" + user_id + "')";
                } else {
                    insertQuery = "INSERT INTO Subscribers (dateRecorded, email, status)"
                           + " values ('" + DateTime.Now + "','" + newEmail + "','" + status + "')";
                }
                Email objmail = new Email();
                objcon.SQLCmd(insertQuery);
                if (!objmail.SendEmail(newEmail, subject, strMsg)) {
                    submitStatusLabel.Text = "please try again...";
                }
            } catch (Exception err) {
                Console.Write("Err msg:" + err.Message);
                submitStatusLabel.Text = "please try again...";
            }
        }
    }
}
